package com.beetlestech.nfe.main.biz.com.impl;

import java.util.ArrayList;
import java.util.List;

import com.beetlestech.nfe.fw.core.biz.AbstractDBLogic;
import com.beetlestech.nfe.fw.core.util.CollectionUtil;
import com.beetlestech.nfe.main.biz.com.HbOrder;
import com.beetlestech.nfe.main.biz.com.dto.GoodsInfoDTO;
import com.beetlestech.nfe.main.biz.com.dto.OrderInfoDTO;
import com.beetlestech.nfe.main.biz.com.dto.OrderItemDTO;
import com.beetlestech.nfe.main.biz.constant.CommonConstant;
import com.beetlestech.nfe.main.biz.constant.CtgCodeConstant;
import com.beetlestech.nfe.main.biz.constant.HbMessageCode;
import com.beetlestech.nfe.main.db.base.StatementDTO;
import com.beetlestech.nfe.main.db.base.StatementUtil;
import com.beetlestech.nfe.main.db.constant.SQLConstant;
import com.beetlestech.nfe.main.db.tbl.OrderdetailDTO;
import com.beetlestech.nfe.main.db.tbl.ShoppingcartDTO;
import com.beetlestech.nfe.main.db.tbl.StudentorderDTO;
import com.beetlestech.nfe.main.db.tbl.StudyhistoryDTO;
import com.beetlestech.nfe.main.db.view.VorderdetailDTO;
import com.beetlestech.nfe.main.db.view.VorderlessonDTO;
import com.beetlestech.nfe.main.db.view.VorderlistDTO;
import com.beetlestech.nfe.main.db.view.VstudentorderDTO;


public class HbOrderImpl extends AbstractDBLogic implements HbOrder, CtgCodeConstant, HbMessageCode, CommonConstant, SQLConstant {
	protected static final String SH_INFO = "订单信息表";

	public OrderItemDTO[] getOrderList(Integer studentID){
		List<OrderItemDTO> orderList = new ArrayList<OrderItemDTO>();
		StatementDTO statementDTO = new StatementDTO();
		StatementUtil.andWhere(statementDTO, VstudentorderDTO.COLUMN_STUDENT_ID, StatementUtil.KEYWORD.EQUAL, studentID);
		VstudentorderDTO listDTO = new VstudentorderDTO();
		listDTO.set_dynamicWhereCondition(statementDTO.getSQLStatement());
		List<VstudentorderDTO> orderInfos = queryDAO.queryForObjectList(VstudentorderDTO.SQL_SEARCH, listDTO);
		for(VstudentorderDTO item : orderInfos){
			orderList.add(convertToOrderInfo(item));
		}
		
		return orderList.toArray(new OrderItemDTO[orderList.size()]);
	}
	
	public void deleteOrder(Integer orderID){
		StudentorderDTO studentorderDTO = new StudentorderDTO();
		studentorderDTO.setorderid(orderID);
		updateDAO.updateOne(StudentorderDTO.DELETE, studentorderDTO, M10190WS, SH_INFO);
	}
	
	public void deleteGoods(Integer orderID, Integer goodsID){
		OrderdetailDTO orderdetail = new OrderdetailDTO();
		orderdetail.setorderid(orderID);
		orderdetail.setgoodsid(goodsID);
		updateDAO.updateOne(OrderdetailDTO.DELETE, orderdetail, M10190WS, SH_INFO);
		
		StatementDTO statementDTO = new StatementDTO();
		StatementUtil.andWhere(statementDTO, OrderdetailDTO.COLUMN_ORDER_ID, StatementUtil.KEYWORD.EQUAL, orderID);
		OrderdetailDTO orderDTO = new OrderdetailDTO();
		orderDTO.set_dynamicWhereCondition(statementDTO.getSQLStatement());
		if(queryDAO.count(OrderdetailDTO.COUNT, orderDTO) == 0){
			deleteOrder(orderID);
		}
	}
	
	public OrderInfoDTO getOrderInfo(Integer orderID){
		StatementDTO statementDTO = new StatementDTO();
		StatementUtil.andWhere(statementDTO, VstudentorderDTO.COLUMN_ORDER_ID, StatementUtil.KEYWORD.EQUAL, orderID);
		VstudentorderDTO studentorderDTO = new VstudentorderDTO();
		studentorderDTO.set_dynamicWhereCondition(statementDTO.getSQLStatement());
		studentorderDTO = queryDAO.queryForObject(VstudentorderDTO.SQL_SEARCH, studentorderDTO);
		
		OrderInfoDTO orderInfo = null;
		
		if(studentorderDTO != null){
			orderInfo = new OrderInfoDTO();
			orderInfo.setorderdate(studentorderDTO.getorderdate());
			orderInfo.setorderid(studentorderDTO.getorderid());
			orderInfo.setordertime(studentorderDTO.getordertime());
			orderInfo.setprice(studentorderDTO.getprice());
			orderInfo.setStateString(studentorderDTO.getcatagorycname());
			if(studentorderDTO.getstate() == Integer.parseInt(CATAGORY_P_ORDER_STATE.PAID.toString())){
				orderInfo.setIsPayed(true);
			}
			else{
				orderInfo.setIsPayed(false);
			}
			
			List<GoodsInfoDTO> goodsList = new ArrayList<GoodsInfoDTO>();
			
			statementDTO = new StatementDTO();
			StatementUtil.andWhere(statementDTO, VorderlistDTO.COLUMN_ORDER_ID, StatementUtil.KEYWORD.EQUAL, orderID);
			VorderlistDTO vlistDTO = new VorderlistDTO();
			vlistDTO.set_dynamicWhereCondition(statementDTO.getSQLStatement());
			List<VorderlistDTO> VOrderInfos = queryDAO.queryForObjectList(VorderlistDTO.SQL_SEARCH, vlistDTO);
			
			for(VorderlistDTO item : VOrderInfos){
				goodsList.add(convertToGoodsInfo(item));
			}
			
			orderInfo.setGoodsInfoDTO(goodsList.toArray(new GoodsInfoDTO[goodsList.size()]));
		}

		return orderInfo;
	}
	
	public void confirmedOrder(Integer orderID){
		updateStudentOrderPayment(orderID);
		insertStudyHistory(orderID);
	}
	
	public void checkOrderDetail(Integer studentID, Integer courseID){
		ShoppingcartDTO shoppingCart = new ShoppingcartDTO();
		shoppingCart.setstudentid(studentID);
		shoppingCart.setcourseid(courseID);
		List<ShoppingcartDTO> lstShopping = queryDAO.queryForObjectList(ShoppingcartDTO.EQUAL_SEARCH, shoppingCart);
		if (!CollectionUtil.isEmpty(lstShopping)) {
			createApplicationException(M10104WC, "购物车");
		}
		
		VorderdetailDTO vorderdetail = new VorderdetailDTO();
		vorderdetail.setstudentid(studentID);
		vorderdetail.setgoodsid(courseID);
		vorderdetail.setgoodstype(Integer.parseInt(GOODS_TYPE.COURSE.toString()));
		List<VorderdetailDTO> lstOrderDetail=queryDAO.queryForObjectList(VorderdetailDTO.EQUAL_SEARCH, vorderdetail);
		if (!CollectionUtil.isEmpty(lstOrderDetail)) {
			createApplicationException(M10104WC, "订单");
		}
	}
	
	private void updateStudentOrderPayment(Integer orderID) {
		StudentorderDTO studentOrder = new StudentorderDTO();
		studentOrder.setorderid(orderID);
		studentOrder.setstate(Integer.parseInt(CATAGORY_P_ORDER_STATE.PAID.toString()));
		studentOrder.setpaymentdate(getCurrentDate());
		studentOrder.setpaymenttime(getCurrentTime());
		studentOrder.setlastdate(getCurrentDate());
		studentOrder.setlasttime(getCurrentTime());
		updateDAO.updateOne(StudentorderDTO.UPDATE, studentOrder, M10190WS, SH_INFO);
	}

	private void insertStudyHistory(Integer orderID){
		VorderlessonDTO orderLesson = new VorderlessonDTO();
		orderLesson.setorderid(orderID);
		List<VorderlessonDTO> lst = queryDAO.queryForObjectList(VorderlessonDTO.EQUAL_SEARCH, orderLesson);
		int i = 0;
		for(VorderlessonDTO item : lst){
			StudyhistoryDTO studyHistory = new StudyhistoryDTO();
			studyHistory.setorderid(item.getorderid());
			studyHistory.setcourseid(item.getcourseid());
			studyHistory.setlessonid(item.getlessonid());
			if(i==0){
				studyHistory.setstate(Integer.parseInt(CATAGORY_P_LESSON.ENABLE.toString()));
			}
			else{
				studyHistory.setstate(Integer.parseInt(CATAGORY_P_LESSON.DISABLE.toString()));
			}
			studyHistory.setisfirstwatch(Integer.parseInt(VIDEO_STATE.NOTFIRSTWATCH.toString()));
			updateDAO.updateOne(StudyhistoryDTO.INSERT, studyHistory, M10190WS, SH_INFO);
			i++;
		}
	}
	
	private GoodsInfoDTO convertToGoodsInfo(VorderlistDTO item) {
		GoodsInfoDTO goodsInfo = new GoodsInfoDTO();
		goodsInfo.setOrderId(item.getorderid());
		goodsInfo.setGoodsId(item.getgoodsid());
		goodsInfo.setName(item.getname());
		goodsInfo.setPath(item.getpath());
		goodsInfo.setCourseEnd(item.getcourseend());
		goodsInfo.setCourseperiod(item.getcourseperiod());
		goodsInfo.setCourseStart(item.getcoursestart());
		goodsInfo.setprice(item.getprice());
		goodsInfo.setDiscount(item.getdiscount() * item.getprice());
		goodsInfo.setTeamIntroduce(item.getteamintroduce());
		return goodsInfo;
	}

	private OrderItemDTO convertToOrderInfo(VstudentorderDTO item){
		OrderItemDTO orderInfo = new OrderItemDTO();
		orderInfo.setOrderDate(item.getorderdate());
		orderInfo.setOrderID(item.getorderid());
		orderInfo.setOrderTime(item.getordertime());
		orderInfo.setPrice(item.getprice());
		if(item.getstate() == Integer.parseInt(CATAGORY_P_ORDER_STATE.PAID.toString())){
			orderInfo.setIsPayed(true);
		}
		else{
			orderInfo.setIsPayed(false);
		}
		orderInfo.setStateString(item.getcatagorycname());
		return orderInfo;
	}
}
